<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Attachs_m extends MY_Model
{

    function __construct()
    {
        parent::__construct();
    }

    public function get_one_attachs_by_id($id)
    {

        $this->db->select('*');
        $this->db->from('attachs');
        $this->db->where('id', $id);
        $query = $this->db->get();
        $info  = $query->row_array();
        return current($info);
    }

    public function get_one_attachs_by($access_key)
    {
        $sql = "SELECT * FROM {$this->db->dbprefix}attachs  where access_key = '$access_key'";
        // echo $sql;exit();
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            $info = $query->result_array();
            return current($info);
        }
    }

    /**
     * @param $id
     * @return mixed
     * 查询单条
     */
    public function get_attachs_by_id($id)
    {

        $this->db->select('*');
        $this->db->from('attachs');
        $this->db->where('id', $id);
        $query = $this->db->get();
        return $query->row_array();
    }

    /**
     * @param $id
     * @param $data
     * @return bool
     * 修改商品信息
     */
    public function sava_attachs($id, $data)
    {

        $this->db->where('id', $id);
        $this->db->update('attachs', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function sava_attachs_by($access_key, $data)
    {

        $this->db->where('access_key', $access_key);
        $this->db->update('attachs', $data);
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    /**
     * @param $data
     * @return bool
     * 新增商品
     */
    public function add_attachs($data)
    {

        $this->db->insert('attachs', $data);
        if ($this->db->affected_rows() > 0) {
            return $this->db->insert_id();
        } else {
            return false;
        }
    }

    /**
     * @return mixed
     * 查询所有商品
     */
    public function get_all_attachs()
    {
        $this->db->select('*');
        $this->db->from('attachs');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * 获取启用的规则
     */
    public function get_attachs_by($access_key)
    {
        $sql = "SELECT * FROM {$this->db->dbprefix}attachs  where access_key = '$access_key'";
        // echo $sql;exit();
        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    /**
     * @param $where
     * @return bool
     * 删除商品
     */
    public function delete_attachs_by($where)
    {
        $this->db->where($where)->delete('attachs');
        return ($this->db->affected_rows() > 0) ? true : false;
    }

    public function get_attachs_by_access($access_key)
    {

        $this->db->select('*');
        $this->db->from('attachs');
        $this->db->where('access_key', $access_key);
        $this->db->order_by('id', 'asc');
        $query = $this->db->get();
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }
}
